use mlua::{UserData, UserDataMethods, LuaSerdeExt};
use serde::{Deserialize,Serialize};

#[derive(Clone)]
pub struct TestModule {
    pub describe:String,
}

impl TestModule{
    pub fn new()->Self{
        TestModule{ describe:"this is a test module".to_string()}
    }
}

impl UserData for TestModule {
    fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
        // methods.add_method("create",|_,test,()|{
        //     Ok(test.clone())
        // });
        methods.add_method_mut("set_describe",|_,test,describe:String|{
            test.describe = describe;Ok(())
        });
        methods.add_method("get_data",|lua,_test,()|{
            let td = TableData::default();
            let td = lua.create_ser_userdata(td).unwrap();
            Ok(td)
        });
        methods.add_method("get_value",|lua,_test,()|{
            let mut td = TableData::default();
            td.id = 13;
            td.name = "hello world".to_string();
            let td = lua.to_value(&td).unwrap();
            Ok(td)
        });
        methods.add_method("get_describe",|_,test,()|{
            Ok(test.describe.clone())
        });
        methods.add_method("print",|_,_test,s:String|{
            wd_log::log_info_ln!("{}",s);
            Ok(())
        });
        methods.add_async_method ("sleep",|_,_test,second:u64|async move{
            tokio::time::sleep(tokio::time::Duration::from_secs(second)).await;
            Ok(())
        })
    }
}
#[derive(Deserialize,Serialize,Default)]
pub struct TableData{
    pub name:String,
    pub id:i64,
    pub slice:Vec<TableData>,
}

impl UserData for TableData{}